<script lang="ts">
export default {
  name: 'memberLevel'
};
</script>

<script setup lang="ts">
import Edit from './components/edit.vue';
import router from '@/router';
import { formatMillisecondsToDate } from '@/utils/utils'
import {
  getMemberLevelList,
  deleteMemberLevel,
} from '@/api/memberLevel';
import { getDictPage } from '@/api/dict'
import { aDownloadFile, aDownloadHref } from '@/utils/utils'


const queryFormRef = ref(ElForm); // 查询表单
const formRef = ref(ElForm); // 用户表单
const defaultTime: [Date, Date] = [
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59)
];
const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);

const queryParams: any = reactive({
  current: 1,
  pageSize: 10,
  startDate: '',
  endDate: '',
  status: null
});

const formData = reactive<any>({
  status: 1,
  defaultStatus: 1,
});

const dialog = reactive<any>({
  visible: false,
});

const dataList = ref([]);
const statusList = ref<any>([]);


function getDict() {
  getDictPage({
    current: 1,
    pageSize: 9999,
    parentId: 6
  }).then((res) => {
    statusList.value = res.data.data
  })
}

/**
 * 查询
 */
function handleQuery() {
  getMemberLevelList({
    ...queryParams,
    status: Number(queryParams.status)
  })
    .then((res: any) => {

      dataList.value = res.data.data;
      total.value = res.data.total;
    })
}
/**
 * 重置查询
 */
function resetQuery() {
  queryParams.endDate = '';
  queryParams.startDate = '';
  queryParams.current = 1;
  queryFormRef.value.resetFields();
  handleQuery();
}


/**
 * 行checkbox change事件
 */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
}


/**
 * 重置表单
 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
}


/**
 * 类型表单弹窗
 *
 */
function openDialog(row?: object) {
  dialog.visible = true;
  if (row) {
    dialog.title = '预览';
    Object.assign(formData, row);
    formData.startEndTime = [formData.startDate, formData.endDate]
  } else {
    //resetForm();
    dialog.title = '预览';
  }
}

/**
 * 删除
 */
function handleDelete(id?: number) {
  let idList = ids.value
  id ? idList = [...ids.value, id] : null
  if (idList.length === 0) {
    ElMessage.warning('请勾选删除项');
    return;
  }

  ElMessageBox.confirm('确认删除已选中的数据项?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteMemberLevel({ ids: idList }).then(() => {
      ElMessage.success('删除成功');
      handleQuery();
    });
  });
}


onMounted(async () => {
  getDict()
  handleQuery(); // 初始化用户列表数据
});

watch(
  () => queryParams.startEndTime,
  (val: any) => {
    if (Array.isArray(val)) {
      queryParams.startDate = val[0];
      queryParams.endDate = val[1];
    } else {
      queryParams.startDate = '';
      queryParams.endDate = '';
    }
  }
);


</script>

<template>
  <div class="app-container">

    <el-row :gutter="24">
      <el-col :lg="24" :xs="24">
        <div class="search">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">

            <el-form-item prop="name" label="等级名称">
              <el-input v-model="queryParams.name" placeholder="请输入" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery">
                <i-ep-search />搜索
              </el-button>
              <el-button @click="resetQuery">
                <i-ep-refresh />
                重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-card shadow="never">
          <template #header>
            <div class="flex justify-between">
              <div>
                <el-button type="success" @click="openDialog()">
                  <i-ep-plus />新增
                </el-button>
                <el-button type="danger" :disabled="ids.length === 0" @click="handleDelete()">
                  <i-ep-delete />删除
                </el-button>
              </div>
            </div>
          </template>

          <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" type="index" align="center" width="55" />
            <el-table-column label="会员名" align="center" prop="name" width="200" />
            <el-table-column label="成长值" align="center" prop="growthPoint" />
            <el-table-column label="默认等级" prop="defaultStatus" align="center">
              <template #default="scope">
                <el-tag v-if="scope.row.defaultStatus == 1" type="success">是</el-tag>
                <el-tag v-else type="info">否</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="免运费标准" align="center" prop="freeFreightPoint" />
            <el-table-column label="每次评价获取的成长值" align="center" prop="commentGrowthPoint" width="200" />
            <el-table-column label="免邮特权" prop="priviledgeFreeFreight" align="center">
              <template #default="scope">
                <el-tag v-if="scope.row.priviledgeFreeFreight == 1" type="success">是</el-tag>
                <el-tag v-else type="info">否</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="签到特权" prop="priviledgeSignIn" align="center">
              <template #default="scope">
                <el-tag v-if="scope.row.priviledgeSignIn == 1" type="success">是</el-tag>
                <el-tag v-else type="info">否</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="评论获奖励特权" prop="priviledgeComment" align="center">
              <template #default="scope">
                <el-tag v-if="scope.row.priviledgeComment == 1" type="success">是</el-tag>
                <el-tag v-else type="info">否</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="专享活动特权" prop="priviledgePromotion" align="center">
              <template #default="scope">
                <el-tag v-if="scope.row.priviledgePromotion == 1" type="success">是</el-tag>
                <el-tag v-else type="info">否</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="会员价格特权" prop="priviledgeMemberPrice" align="center">
              <template #default="scope">
                <el-tag v-if="scope.row.priviledgeMemberPrice == 1" type="success">是</el-tag>
                <el-tag v-else type="info">否</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="会员生日特权" prop="priviledgeBirthday" align="center">
              <template #default="scope">
                <el-tag v-if="scope.row.priviledgeBirthday == 1" type="success">是</el-tag>
                <el-tag v-else type="info">否</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" align="center" width="250">
              <template #default="scope">
                <el-button type="primary" link size="small" @click.stop="handleDelete(scope.row.id)">
                  <i-ep-delete />删除
                </el-button>
                <el-button type="primary" link size="small" @click.stop="openDialog(scope.row)">
                  <i-ep-edit />编辑
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.current"
            v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
        </el-card>
      </el-col>
    </el-row>
    <Edit :dialog="dialog" :formData="formData" @resetQuery="resetQuery"></Edit>
  </div>
</template>
<style lang="scss" scoped>
.column-img {
  width: 100px;
  height: 100px;

}
</style>